package com.example.demo.service.impl;

import com.example.demo.dao.InquiryMapper;
import com.example.demo.model.*;
import com.example.demo.model.vo.InquiryAddVo;
import com.example.demo.model.vo.InquiryPartsVo;
import com.example.demo.model.vo.InquirySupplierVo;
import com.example.demo.model.vo.PartsCerteriaVo;
import com.example.demo.repository.PartsRepository;
import com.example.demo.service.InquiryService;
import com.example.demo.service.PartsService;
import com.example.demo.util.AllRecords;
import com.example.demo.util.Result;
import com.example.demo.util.ResultVO;
import com.querydsl.core.types.dsl.Expressions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by zkq19 on 2018/7/1.
 */
@Service
public class InquiryServiceImpl implements InquiryService{
    @Autowired
    private InquiryMapper inquiryMapper;
    /**
     * 询价单新增借口
     * @param inquiryAddVo
     * @return
     */
    @Override
    public Result<InquiryAddVo> addInquiry(InquiryAddVo inquiryAddVo) {

        //验证数据是否合法
        validata(inquiryAddVo);
        //操作数据库
        InquiryTotal inquiryTotal = new InquiryTotal();
        BeanUtils.copyProperties(inquiryAddVo,inquiryTotal);
        Date nowdate = new Date();
        inquiryTotal.setCreateId(2031);
        inquiryTotal.setCreateName("测试");
        inquiryTotal.setCreateTime(nowdate);
        inquiryTotal.setInquiryNo(nowdate+ UUID.randomUUID().toString());
        inquiryTotal.setUpdateId(2031);
        inquiryTotal.setUpdateName("测试");
        inquiryTotal.setUpdateTime(nowdate);
        inquiryMapper.save(inquiryTotal);
        System.out.print(inquiryTotal.getId());
        //获取备件集合
        /*List<InquiryPartsVo> inquiryPartsVos = inquiryAddVo.getInquiryPartsVos();
        List<InquiryParts> inquiryParts = new ArrayList<InquiryParts>();
        BeanUtils.copyProperties(inquiryPartsVos,inquiryParts);
        for (InquiryParts inquiryPart : inquiryParts) {
            inquiryPart.setInquiryId(inquiryTotal.getId());
            inquiryPart.setCreateId(2031);
            inquiryPart.setCreateName("测试");
            inquiryPart.setCreateTime(nowdate);
            inquiryPart.setUpdateId(2031);
            inquiryPart.setUpdateName("测试");
            inquiryPart.setUpdateTime(nowdate);
        }
        inquiryMapper.saveIquiryParts(inquiryParts);
        //供应商
        List<InquirySupplierVo> inquirySupplierVos = inquiryAddVo.getInquirySupplierVos();
        List<InquirySupplier> inquirySuppliers = new ArrayList<InquirySupplier>();
        BeanUtils.copyProperties(inquirySupplierVos,inquirySuppliers);
        for (InquirySupplier inquirySupplier : inquirySuppliers) {
            inquirySupplier.setInquiryId(inquiryTotal.getId());
            inquirySupplier.setCreateId(2031);
            inquirySupplier.setCreateName("测试");
            inquirySupplier.setCreateTime(nowdate);
            inquirySupplier.setUpdateId(2031);
            inquirySupplier.setUpdateName("测试");
            inquirySupplier.setUpdateTime(nowdate);
        }
        inquiryMapper.saveIquirySupplier(inquirySuppliers);*/
        return ResultVO.success(inquiryAddVo);
    }

    @Override
    public Result<InquiryAddVo> updateInquiry(InquiryAddVo inquiryAddVo) {
        //验证数据是否合法
        validata(inquiryAddVo);
        //操作数据入库
        //1.修改主表的数据
        //2.删除此询价单下备件的数据
        inquiryMapper.deletePartsById(inquiryAddVo.getId());
        //3.删除此询价单下供应商的数据
        //4.新增备件表中的数据
        //5.新增供应商表中的数据
        return null;
    }


    private Result<InquiryAddVo> validata(InquiryAddVo inquiryAddVo) {
        //备件
        List<String> partsCodeList = new ArrayList<String>();
        for(InquiryPartsVo inquiryPartsVo:inquiryAddVo.getInquiryPartsVos()){
            partsCodeList.add(inquiryPartsVo.getPartsCode());
        }
        //供应商
        List<String> supplierCodeList = new ArrayList<String>();
        for(InquirySupplierVo InquirySupplierVo:inquiryAddVo.getInquirySupplierVos()){
            supplierCodeList.add(InquirySupplierVo.getSupplierCode());
        }
        //验证备件编码,供应商是否重复
        Result<InquiryAddVo> inquiryAddVoResult = validatePartsSupplierRepeat(inquiryAddVo,partsCodeList,supplierCodeList);
        if(!inquiryAddVoResult.getStatus()){
            return ResultVO.error(inquiryAddVoResult.getCode(),inquiryAddVoResult.getMsg());
        }
        //验证备件编码,供应商是否存在
        validatePartsSupplierExist(inquiryAddVo,partsCodeList,supplierCodeList);
        //验证供应商是否合法的供应商
        validatePartsSupplierLegal(inquiryAddVo,partsCodeList,supplierCodeList);
        return ResultVO.success(inquiryAddVo);
    }

    private Result<InquiryAddVo> validatePartsSupplierLegal(InquiryAddVo inquiryAddVo, List<String> partsCodeList, List<String> supplierCodeList) {
        List<InquiryParts> inquiryParts = inquiryMapper.quryPartsBySupplierCode(supplierCodeList);
        List<String> partsCode = new ArrayList<String>();
        for (InquiryParts inquiryParts1 : inquiryParts) {
            partsCode.add(inquiryParts1.getPartsCode());
        }
        boolean b = partsCode.containsAll(partsCodeList);
        for (String code:partsCodeList){
            if(!partsCode.contains(code)){
                return ResultVO.error( 500, "供应商不合法,不合法的供应商为:"+code);
            }
        }
        return ResultVO.success(inquiryAddVo);
    }

    private Result<InquiryAddVo> validatePartsSupplierExist(InquiryAddVo inquiryAddVo,List<String> partsCodeList,List<String> supplierCodeList) {
        //备件
        List<InquiryParts> inquiryParts = inquiryMapper.quryPartsByCode(partsCodeList);
        List<String> partsCodes = new ArrayList<String>();
        for (InquiryParts parts : inquiryParts) {
            partsCodes.add(parts.getPartsCode());
        }

        if (partsCodes.size() !=partsCodeList.size() ) {
            partsCodeList.remove(partsCodes);
            return ResultVO.error( 500, "备件编码不存在,不存在编码为:"+partsCodeList);
        }

        //供应商
        List<InquirySupplier> inquirySuppliers = inquiryMapper.qurySuppliersByCode(supplierCodeList);
        List<String> supplierCodes = new ArrayList<String>();
        for (InquirySupplier supplier : inquirySuppliers) {
            supplierCodes.add(supplier.getSupplierCode());
        }
        if (supplierCodes.size() !=supplierCodeList.size() ) {
            supplierCodeList.remove(supplierCodes);
            return ResultVO.error( 500, "供应商不存在,不存在供应商为:"+supplierCodeList);
        }

        return ResultVO.success(inquiryAddVo);
    }

    private Result<InquiryAddVo> validatePartsSupplierRepeat(InquiryAddVo inquiryAddVo,List<String> partsCodeList,List<String> supplierCodeList) {
        //备件编码
        List<InquiryPartsVo> inquiryPartsVos = inquiryAddVo.getInquiryPartsVos();
        partsCodeList = new ArrayList(new HashSet(partsCodeList));
        if(partsCodeList.size()!=inquiryPartsVos.size()){
            inquiryPartsVos.remove(partsCodeList);
            return ResultVO.error( 500, "备件编码重复,重复编码为:"+inquiryPartsVos);
        }
        //供应商
        List<InquirySupplierVo> inquirySupplierVos = inquiryAddVo.getInquirySupplierVos();
        supplierCodeList = new ArrayList(new HashSet(supplierCodeList));
        if(supplierCodeList.size()!=inquirySupplierVos.size()){
            inquirySupplierVos.remove(supplierCodeList);
            return ResultVO.error( 500, "备件编码重复,重复编码为:"+inquirySupplierVos);
        }
        return ResultVO.success(inquiryAddVo);
    }
}
